package arrAndstr;

import java.util.Stack;

/*
    20. 有效的括号
 */
public class T20 {

    public static void main(String[] args) {

    }

    public static boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();

        for (char ch : chars) {
            if (ch=='(' || ch=='{' || ch=='[') {
                stack.push(ch);
            }
            if (ch==')') {
                if (stack.isEmpty())    return false;
                if (stack.peek()=='(')  stack.pop();
                else stack.push(ch);
            } else if (ch=='}') {
                if (stack.isEmpty())    return false;
                if (stack.peek()=='{')  stack.pop();
                else stack.push(ch);
            } else if (ch==']') {
                if (stack.isEmpty())    return false;
                if (stack.peek()=='[')  stack.pop();
                else stack.push(ch);
            }
        }

        return stack.isEmpty();
    }

}
